import lodashUniqBy from 'lodash.uniqby';
import shortUUID from 'short-uuid';
import moment from 'moment';

export function fillData(source = {}, data = {}) {
  var keys = Object.keys(source);
  for (let index = 0; index < keys.length; index++) {
    const key = keys[index];
    source[key] = data[key];
    
  }
}

export function createUuid() {
  return shortUUID().uuid();
}

export const createUUID = createUuid;


export function safeParseJson(str) {
  try {
    if (str == '') return '';
    return JSON.parse(str);
  } catch (e) {
    return undefined;
  }
  
}

export function safeParseInt(str) {
  
  if (!str) {
    return '';
  }
  return parseInt(str);
}

export function safeParseFloat(str) {
  if (!str) {
    return '';
  }
  return parseFloat(str);
}


export function getUrlParam(parameterName, currentUrl = location.href) {
  var rs = new RegExp('(^|[&,?])' + parameterName + '=([^\&]*)(\&|$)', 'gi')
    .exec(currentUrl), tmp;
  if (tmp = rs) return tmp[2];
  return null;
};


/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
import jsCookie from 'js-cookie';

export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone');
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
}


export function uniqBy(array, key) {
  return lodashUniqBy(array, key);
}


export function validatePromise(domstr) {
  return new Promise(resolve => {
    this.$refs[domstr].validate(isyes => {
      resolve(isyes);
    });
  });
}


export function dateFormatYYYYMMDD(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('YYYY-MM-DD');
}

export function dateFormatYYYYMMDDHH(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('YYYY/MM/DD:HH');
}


export function dateFormatYYYYMM(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('YYYY-MM');
}

export function dateFormatMMDD(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('MM-DD');
}

export function dateFormatMMDDHHmm(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('MM-DD HH:mm');
}

export function dateFormatYYYYMMDDHHmmss(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
}


export function dateFormatYYYYMMDDHHmm(value) {
  if (!value) return '';
  value = value.toString();
  if (value && value.indexOf('1970-01-01') >= 0) {
    return '';
  }
  return moment(value).format('YYYY-MM-DD HH:mm');
}

export function nowWithFormat(format = 'YYYY-MM-DD HH:mm:ss') {
  return moment().format(format);
}


export function getToken() {
  return jsCookie.get('authToken');
}

export function clearZero(obj) {
  for (const argumentsKey in obj) {
    if (obj[argumentsKey] == 0) {
      obj[argumentsKey] = '';
    }
    
  }
}

export function createA(href, target) {
  var a = document.createElement('a');
  a.setAttribute('href', href);
  a.setAttribute('target', target || '_blank');
  a.setAttribute('id', 'startTelMedicine');
  // 防止反复添加
  if (document.getElementById('startTelMedicine')) {
    document.body.removeChild(document.getElementById('startTelMedicine'));
  }
  document.body.appendChild(a);
  a.click();
}
